﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Data;

namespace PayRoller.Include
{
    public partial class UClientDisplay : System.Web.UI.UserControl
    {
        public int OrgID { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                this.DisplaySettingsStatus();
                this.BindInfo();
            }
        }

        public void BindInfo()
        {
            if (OrgID > 0)
            {
                this.BindClientInfo();
                this.BindEmployeeInfo();
            }
        }

        private void BindClientInfo()
        {
            using (TorginfoBN obn = new TorginfoBN(this.Page))
            {
                TorginfoDT detail = obn.Get(this.OrgID);
                if (detail != null)
                {
                    this.labCreditLimit.Text= detail.CreditLimit.ToString();
                    this.labClientName.Text = detail.OrgName;
                    this.labOurReference.Text = detail.Sid.ToString();

                    ////以下两个数值，需要从系统内实时求取（不能从表内读取）
                    //decimal closingBalance= Convert.ToDecimal(TorginfoBN.GetClosingBalance(this.OrgID));
                    //this.labCreditBalance.Text = closingBalance.ToString();
                    //this.labAvailableCredit.Text = (detail.CreditLimit - closingBalance).ToString();
                    this.labCreditBalance.Text = detail.CreditBalance.ToString();
                    this.labAvailableCredit.Text = detail.AvailableBalance.ToString();
                }
            }
        }

        private void BindEmployeeInfo()
        {
            using (TempinfoBN obn2 = new TempinfoBN(this.Page))
            {
                //需要根据当前企业ID获取其下的员工列表
                obn2.QueryOrgSid(this.OrgID);
                DataTable table = obn2.GetList();
                this.dlEmployee.DataSource = table;
                this.dlEmployee.DataBind();
            }
        }

        /// <summary>
        /// 本方法设置了 需要显示隐藏的部分配置项；其他的配置项在 DataList的绑定事件中设置。
        /// </summary>
        private void DisplaySettingsStatus()
        {
            ReportSettingsClientDT setting = GetReportSettings();
            if (setting != null)
            {
                this.trAvailableCredit.Visible = setting.IsDisplayAvailableCredit;
                this.trClientName.Visible = setting.IsDisplayOrgName;
                this.trCreditBalance.Visible = setting.IsDisplayCreditBalance;
                this.trCreditLimit.Visible = setting.IsDisplayCreditLimit;
                this.trOurReference.Visible = setting.IsDisplaySystemReference;
                this.dlEmployee.Visible = setting.IsDisplayEmployeeList;
            }
        }

        private ReportSettingsClientDT GetReportSettings()
        {
            ReportSettingsBN business = new ReportSettingsBN(this.Page);
            ReportSettingsClientDT setting = business.GetReportSettingsClient(0);
            return setting;
        }

        

        protected void dlEmployee_ItemCreated(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType= e.Item.ItemType;
            ReportSettingsClientDT setting = GetReportSettings();
            switch (itemType)
            { 
                case ListItemType.Header:
                    e.Item.FindControl("thEmployeeID").Visible = setting.IsDisplayEmployeeID;
                    e.Item.FindControl("thEmployeeName").Visible = setting.IsDisplayEmployeeName;
                    break;
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:
                    e.Item.FindControl("tdEmployeeID").Visible = setting.IsDisplayEmployeeID;
                    e.Item.FindControl("tdEmployeeName").Visible = setting.IsDisplayEmployeeName;
                    break;
            }
        }
    }
}